package com.example.freeengine.wallpaper.alarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

import com.example.freeengine.wallpaper.constants.WallPaperConstant;
import com.example.freeengine.wallpaper.constants.WallPaperUpdateStrategy;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * @author Terry Deng
 */
public class MyAlarmWorker {
    private static final String TAG = MyAlarmWorker.class.getSimpleName();
    private static AlarmManager alarmMgr;

    private MyAlarmWorker(){}

    public static class Builder {
        private final Context context;

        public Builder(Context context){
            this.context = context;
            if (alarmMgr == null){
                alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            }
        }

        public void startEveryDayRefreshAlarm(){
            stopAlarm();
            Log.i(TAG, "start everyday Alarm");
            Intent intent = new Intent(context, MyAlarmWorkerReceiver.class);
            intent.putExtra(WallPaperConstant.KEY_ALARM_WORKER_RECEIVER_EXTRA_STRATEGY, WallPaperUpdateStrategy.FROM_EVERYDAY.name());
            PendingIntent alarmEveryDayIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.setTimeZone(TimeZone.getTimeZone("Asia/Chongqing"));
            calendar.set(Calendar.HOUR_OF_DAY, 9);

//            触发时间。如果您指定的触发时间为过去的时间，则闹钟会立即触发。
//            闹钟触发的待定 Intent。当您设置使用同一待定 Intent 的第二个闹钟时，它会替换原始闹钟。
            alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, alarmEveryDayIntent);
        }

        public void startCycleRefreshRefreshAlarm(long intervalMillis){

            stopAlarm();
            Log.i(TAG, "start cycle refresh Alarm");
            Intent intent = new Intent(context, MyAlarmWorkerReceiver.class);
            intent.putExtra(WallPaperConstant.KEY_ALARM_WORKER_RECEIVER_EXTRA_STRATEGY, WallPaperUpdateStrategy.FROM_CYCLEREFRESH.name());
            PendingIntent alarmCycleRefreshIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);
            alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    intervalMillis, alarmCycleRefreshIntent);
        }

        public void stopAlarm(){
            Intent intent = new Intent(context, MyAlarmWorkerReceiver.class);
            intent.putExtra(WallPaperConstant.KEY_ALARM_WORKER_RECEIVER_EXTRA_STRATEGY, WallPaperUpdateStrategy.FROM_EVERYDAY.name());
            PendingIntent existEveryDayIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_IMMUTABLE);
            if (alarmMgr != null && existEveryDayIntent != null) {
                Log.i(TAG, "stop everyday Alarm");
                alarmMgr.cancel(existEveryDayIntent);
            }

            Intent intent2 = new Intent(context, MyAlarmWorkerReceiver.class);
            intent2.putExtra(WallPaperConstant.KEY_ALARM_WORKER_RECEIVER_EXTRA_STRATEGY, WallPaperUpdateStrategy.FROM_CYCLEREFRESH.name());
            PendingIntent existCycleRefreshIntent = PendingIntent.getBroadcast(context, 1, intent2, PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_IMMUTABLE);
            if (alarmMgr != null && existCycleRefreshIntent != null) {
                Log.i(TAG, "stop cycle refresh Alarm");
                alarmMgr.cancel(existCycleRefreshIntent);
            }
        }
    }
}
